package com.controller;

import com.constant.RedisKey;
import com.po.UserPO;
import com.service.MenuServiceImpl;
import com.util.*;
import com.vo.MenuVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 获取菜单的数据
 */
@RestController
public class MenuController {
    @Autowired
    private MenuServiceImpl menuServiceImpl;
    @Autowired
    private RedisUtil redisUtil;

    //private Logger logger =  LoggerFactory.getLogger(this.getClass());

    @RequestMapping("/getAllMenu")
    public Result<List<MenuVo>> getMenuByMid(HttpServletRequest request) {
        String token = request.getHeader("userToken");
        String tokenKey = RedisKey.LIBRARY_USER_TOKEN + token;
        String adminJson = redisUtil.get( tokenKey );//从token里面获取数据
        if( StringUtils.isBlank( adminJson ) ){//判断uid
            throw new RestException("请登录");
        }
        UserPO userPO = JSONUtils.parse( adminJson , UserPO.class );

        //从redis中获取回来
        List<MenuVo> menus = menuServiceImpl.getMenuByMember(userPO);
        return ResultUtil.returnDataSuccess(menus);
    };

//    @RequestMapping("/getMenuByPid")
//    public Result<List<MenuVo>> getMenuByPid(@RequestBody MenuVo vo){
//        return ResultUtil.returnDataSuccess(this.menuService.getMenuByPid(vo.getParentId()));
//    }

}